package com.example.servicetest

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.example.servicetest.util.ColorUtil
import kotlin.math.abs
import kotlin.math.hypot
import kotlin.math.roundToInt

class DanceView:View {

    /**
     * 单个格子宽度
     */
    private var mBoxWith: Float = 0f

    /**
     * 单个格子高度
     */
    private var mBoxHeight: Float = 0f

    private val mBoxRowCount:Int = 30

    private val mBoxColumnCount:Int = 30

    private val mBoxGap:Float = 2f

    private val maxWave:Int = 150

    private val mPaintBox = Paint()

    private var mBoxRect: Rect = Rect()

    private lateinit var mExpandCountArray:IntArray

    private var mColorsCountArray:IntArray = IntArray(mBoxColumnCount)

    constructor(context: Context) : this(context,null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs,0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ){
        for (i in mColorsCountArray.indices){
            mColorsCountArray[i]= Color.parseColor(ColorUtil.ranDomColor)
        }
    }

    fun updateFFT(fft: ByteArray){
        val model = FloatArray(fft.size / 2 + 1)
        model[0] = abs(fft[1].toInt()).toFloat()
        var j = 1
        var i = 2
        while (i < mBoxColumnCount * 2) {
            model[j] = hypot(fft[i].toDouble(), fft[i + 1].toDouble()).toFloat()
            i += 2
            j++
            model[j] = abs(model[j])
        }
        mExpandCountArray  = IntArray(fft.size / 2 + 1)
        for(index in model.indices){
            val ratio = model[index]/maxWave
            val count = (ratio*(mBoxColumnCount/2-1)).toInt()
            mExpandCountArray[index] = count
        }
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //计算单个格子的宽高
        mBoxWith =
            (width - (mBoxGap * (mBoxRowCount - 1) - paddingStart - paddingEnd)) / mBoxRowCount
        mBoxHeight =
            (height - (mBoxGap * (mBoxColumnCount - 1) - paddingTop - paddingBottom)) / mBoxColumnCount
        drawCell(canvas)
    }

    /**
     * 画方格
     */
    private fun drawCell(canvas: Canvas) {
        mPaintBox.run {
            isAntiAlias = true
            style = Paint.Style.FILL
        }
        for (rowIndex in 0 until mBoxColumnCount) {
            for (columnIndex in 0 until mBoxRowCount) {
                mBoxRect.set(
                    (paddingStart.toFloat() + mBoxWith * rowIndex + mBoxGap * rowIndex).roundToInt(),
                    (paddingTop.toFloat() + mBoxHeight * columnIndex + mBoxGap * columnIndex).roundToInt(),
                    (paddingStart + mBoxWith * (rowIndex + 1) + mBoxGap * rowIndex).roundToInt(),
                    (paddingTop + mBoxHeight * (columnIndex + 1) + mBoxGap * columnIndex).roundToInt(),
                )
                var inaccessiblePosition = 0
                var inaccessiblePosition1 = 0
                if(this::mExpandCountArray.isInitialized){
                    inaccessiblePosition = mBoxColumnCount/2 - mExpandCountArray[rowIndex] - 1
                    inaccessiblePosition1 = mBoxColumnCount/2 + mExpandCountArray[rowIndex] + 1
                }

                if(columnIndex<=inaccessiblePosition || columnIndex>=inaccessiblePosition1){
                    mPaintBox.color = ContextCompat.getColor(context,R.color.color000000)
                }else{
                    mPaintBox.color = mColorsCountArray[rowIndex]
                }

                canvas.drawRect(mBoxRect, mPaintBox)

                mBoxRect.setEmpty()
            }
        }
    }

}